package com.yicong.ants.utils;

import android.view.View;
import android.widget.ImageView;

import com.yicong.ants.R;

public class GroupLayoutHelp implements View.OnClickListener {

    private ImageView imageView;
    private View groupView, item1, item2;
    private int selectPosition = 1;
    private OnSelectClickListener onSelectClickListener;

    public GroupLayoutHelp(View view) {
        groupView = view;
        imageView = groupView.findViewById(R.id.group_image);
        item1 = groupView.findViewById(R.id.item1);
        item2 = groupView.findViewById(R.id.item2);
    }

    public GroupLayoutHelp setOnSelectClickListener(OnSelectClickListener onSelectClickListener) {
        this.onSelectClickListener = onSelectClickListener;
        return this;
    }

    public int getSelected() {
        return selectPosition;
    }

    public void performClick(int i) {
        if (i < 1 || i > 2) {
            return;
        }
        selectPosition = i;
        select();
    }


    public GroupLayoutHelp ok() {
        select();
        item1.setOnClickListener(this);
        item2.setOnClickListener(this);
        return this;
    }

    private void select() {
        if (selectPosition == 1) {
            imageView.setImageResource(R.mipmap.btn_top_item_left);
        } else {
            imageView.setImageResource(R.mipmap.btn_top_item_right);
        }
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.item1) {
            if (selectPosition != 1) {
                selectPosition = 1;
                select();
                if (onSelectClickListener != null) {
                    onSelectClickListener.onSelected(selectPosition);
                }
            }
        } else if (view.getId() == R.id.item2) {
            if (selectPosition != 2) {
                selectPosition = 2;
                select();
                if (onSelectClickListener != null) {
                    onSelectClickListener.onSelected(selectPosition);
                }
            }
        }
    }

    public interface OnSelectClickListener {
        void onSelected(int position);
    }
}
